package hk.xing.boot.controller;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import hk.xing.boot.dto.SysUserLoginDto;
import hk.xing.boot.entity.Role;
import hk.xing.boot.entity.SysUser;
import hk.xing.boot.enums.APIResponseEnum;
import hk.xing.boot.pojo.ApiResponse;
import hk.xing.boot.service.SysUserService;
import hk.xing.boot.vo.SysUserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/auth")
@Schema(name = "Authorization", description = "授权控制器")
@CrossOrigin
public class AuthController {

    @Resource
    private SysUserService sysUserService;

    @Operation(summary = "用户登录", description = "根据用户提供的账号密码进行登录")
    @PostMapping("/login")
    public ApiResponse<HashMap<String,String>> login(@RequestBody SysUserLoginDto loginDto) throws Exception {
        //不允许入参为空
        if (loginDto == null || loginDto.getAccount() == null || loginDto.getPassword() == null) {
            return ApiResponse.fail(APIResponseEnum.LOGIN_NOT_ALLOW_NULL);
        }
        try{
            //执行用户信息校验
            SysUser sysUser = sysUserService.validateLoginUser(loginDto.getAccount(), loginDto.getPassword());
            //如果校验信息没有问题，则可以下发token
            if (ObjectUtil.isNotEmpty(sysUser)) {
                HashMap<String,String> response = new HashMap<>();
                String token = StpUtil.getTokenInfo().getTokenValue();
                response.put("token", token);
                return ApiResponse.success(response);
            }
        } catch (Exception e) {
            return ApiResponse.fail(APIResponseEnum.LOGIN_PASSWORD_ERROR);
        }
        return ApiResponse.fail(APIResponseEnum.LOGIN_FAIL);
    }


    @Operation(summary = "用户注册")
    @PostMapping("signUp")
    public ApiResponse<String> signUp(@RequestBody SysUser sysUser) throws Exception {
        sysUserService.doSignUp(sysUser);
        return ApiResponse.success("success");
    }


    @Operation(summary = "用户登出")
    @GetMapping("/logout")
    public ApiResponse<String> logout() {
        sysUserService.logout();
        return ApiResponse.success("success");
    }


    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("/getCurrentUser")
    public ApiResponse<SysUserVo> getCurrentUser() {
        SysUserVo currentLoginSysuser = sysUserService.getCurrentUser();
        return ApiResponse.success(currentLoginSysuser);
    }


    @Operation(summary = "获取当前登录用户权限信息")
    @GetMapping("/getPermission")
    public ApiResponse<Set<String>> getCurrentUserPermission() {
        return ApiResponse.success(sysUserService.getCurrentUserPermission());
    }


    @Operation(summary = "获取当前登录用户权限信息")
    @GetMapping("/getRoles")
    public ApiResponse<SysUserVo> getCurrentUserRoles() {
        return ApiResponse.success(sysUserService.getCurrentUserRoles());
    }
}
